package com.outfood.utils;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
 * @Author huisonglin
 * @Description
 * @PackageName outfoodxia-master
 * @Package com.outfood.utils
 * @Date 2024-05-17 10:44
 * @Version 1.0
 */
public class JavaScriptUtil {
    // 创建一个ScriptEngineManager实例
    public static ScriptEngineManager manager = new ScriptEngineManager();
    // 获取JavaScript引擎
    public static ScriptEngine engine = manager.getEngineByName("nashorn");


    public static String createRequestId() {

        try {
            // 执行JavaScript代码
            Object result = engine.eval("\t\tfunction v() {\n" +
                    "\t\t    return \"xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx\".replace(/[xy]/g, (function(e) {\n" +
                    "\t\t        var t = 16 * Math.random() % 16 | 0;\n" +
                    "\t\t        return (\"x\" === e ? t : 3 & t | 8).toString(16)\n" +
                    "\t\t    }\n" +
                    "\t\t    )).toUpperCase() + \"|\" + Date.now()\n" +
                    "\t\t} v()");
            // 输出JavaScript返回值
            return result.toString();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }

}
